Type MapWindow Extends ToolWindow

	Field MapAsset : Map
	
	Method New()
	
		ToolType = TOOL_TYPE_MAP
	
	End Method 

	Method Update(EID:Int,Obj:Object,EEX : Object = Null)
		Select EID	
			Case EVENT_GADGETACTION
				Select Obj
				
					Case Gadgets[14]
						If MapAsset.FileBank = Null Then 
							MapAsset.FileBank = New TBank
						EndIf
						MapAsset.ShortName = TextFieldText(Gadgets[6])
						If MapAsset.ShortName = "" Then MapAsset.ShortName = "Untitled_"+Rand(1,9999)
						MapAsset.Map_W = Int(TextFieldText(Gadgets[3]))
						MapAsset.Map_H = Int(TextFieldText(Gadgets[4]))
						
						'TileAsset.TilesetFromBank(TileAsset.FileBank,Int(TextFieldText(Gadgets[8])),Int(TextFieldText(Gadgets[4])), Int(TextFieldText(Gadgets[6])), Int(GadgetExtra(AssetNode).ToString())|DYNAMICIMAGE)
						MapAsset.AssetParent = Asset(GadgetExtra(AssetNode))
						AddTreeViewNode( MapAsset.ShortName,AssetNode,1,MapAsset)
						DebugLog "Path: " + GadgetText(AssetNode)+MapAsset.ShortName
						If MapAsset.AssetPath = "" Then
							DebugLog "new Path: "+MapAsset.FetchAssetPath()+"\"+MapAsset.ShortName
							
							MapAsset.AssetPath = MapAsset.FetchAssetPath()+"\"+MapAsset.ShortName
						EndIf
						SaveBank(MapAsset.FileBank,"Projects\"+CurrentProject+"\"+MapAsset.AssetPath+".map")
						MapAsset.Save()
						For Local Gad : TGadget = EachIn Gadgets
						
							If Gad <> Null Then
								FreeGadget Gad
								'ListRemove(Gadgets,Gad)
							EndIf
						Next
						FreeGadget Window
						Gadgets = Null
						ListRemove(ParentList,Self)						
					
										
				
				
				End Select
		End Select	
	
		Super.Update(EID,Obj)
	
	End Method

	Method Set()
		Local Gads : TGadget[16]
		If MapAsset = Null Then MapAsset = New Map
		Window = CreateWindow( Title,Pos.x,Pos.y,Pos.w,Pos.h,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )
		Gads[5] = CreateLabel("Name:",10,10,40,20,Window)
		Gads[6] = CreateTextField(50,10,100,20,Window)
		SetGadgetText(Gads[6],MapAsset.ShortName)
		Gads[0] = CreatePanel(0,30,ClientWidth(Window),80,Window,PANEL_GROUP,"Dimensions")		
		Gads[1] = CreateLabel("Width:",10,10,40,20,Gads[0])
		Gads[2] = CreateLabel("Height:",10,30,40,20,Gads[0])
		Gads[3] = CreateTextField(50,10,50,20,Gads[0])
		SetGadgetText(Gads[3],MapAsset.Map_W)
		Gads[4] = CreateTextField(50,30,50,20,Gads[0])
		SetGadgetText(Gads[4],MapAsset.Map_H)
		Gads[7] = CreatePanel(0,120,ClientWidth(Window),120,Window,PANEL_GROUP,"Media")			
		Gads[8] = CreateLabel("Background:",10,10,100,20,Gads[7])
		Gads[9] = CreateComboBox(10,30,100,20,Gads[7])
		Gads[10] = CreateLabel("Music",110,10,100,20,Gads[7])
		Gads[11] = CreateComboBox(110,30,100,20,Gads[7])
		Gads[12] = CreateLabel("Light Color",10,60,100,20,Gads[7])
		Gads[13] = CreatePanel(110,60,30,30,Gads[7])	
		SetPanelColor(Gads[13],MapAsset.LR,MapAsset.LG,MapAsset.LB)
		
		If EditMode = True Then
			Gads[14] = CreateButton("Save Map",10,250,100,20,Window)
		Else
			Gads[14] = CreateButton("Create Map",10,250,100,20,Window)
		EndIf
		Gads[15] = CreateButton("Cancel",120,250,100,20,Window)
			
		Gadgets = Gads
		
		
	
	End Method

End Type